﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Ledict.Core.Data;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    public class CreateIndex
    {
        /// <summary>
        /// 得到建立检索的所需的数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetIndex(List<string> HeadWords) // and  d.HW_AllParentIDs like '@HeadWords%' and Is_Index=0 
        {
          
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"           select    top 5000 b.CaseId , b. PropertyId, b.JudicialProcedureId  ,b.CourtId    from dbo.t_Case as b 
           where   
             Is_Index=0 and (1=0 
            ");
            foreach (var item in HeadWords)
            {
                strSql.Append(@" or d.HW_AllParentIDs like  '" + item + "%'  ");
            }
            strSql.Append(@")  ");
          
            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString ());
         
          
            DataSet oSet = oBase.ExecuteDataSet(oCmd);

            if (oSet.Tables.Count > 0)
            {
                return oSet.Tables[0];
            }
            return null;
        }

        //left join  dbo.t_Case_HeadWords_Associate as c        //    on  c.CaseId=b.CaseId        //    left join  dbo.t_Case_HeadWords  as d         //    on c.HeadWordId=d.Id

        public int GetSumCount( ) //isnull(b.Is_Index,0) =0 and
        {


//            StringBuilder strSql = new StringBuilder();
//            strSql.Append(@" select   count(a.Id) //            from dbo.t_Case_ParagraphLogic  as a //            left join dbo.t_Case as b //            on a.CaseId=b.CaseId  //           left join  dbo.t_Case_HeadWords_Associate as c//            on  c.CaseId=b.CaseId//            left join  dbo.t_Case_HeadWords  as d //            on c.HeadWordId=d.Id////           where  ParagraphTypeId in  (140,150)//           and  d.HW_AllParentIDs like'1000,1179,1181%'  and Is_Index=0
//           
//             ");

//            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
//            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
//            oBase.AddInParameter(oCmd, "@HeadWords", DbType.String, HeadWords);

          //  return Convert.ToInt32(oBase.ExecuteScalar(oCmd));
            return 5000;
        }


        public DataTable GetHeadWords(string CaseId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
            select    (select  HW_AllParentIDs  from dbo.t_Case_HeadWords where  Id = a. HeadWordId)as HW_AllParentIDs 
            from dbo.t_Case_HeadWords_Associate as a 
            where CaseId=@CaseId 
");

            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
           oBase.AddInParameter(oCmd, "@CaseId", DbType.String, CaseId);
            DataSet oSet = oBase.ExecuteDataSet(oCmd);

            if (oSet.Tables.Count > 0)
            {
                return oSet.Tables[0];
            }
            return null;
        }

        public string GetParagraphText(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
            select ParagraphText from  t_Case_ParagraphLogic where id=@id
");

            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@id", DbType.String, id);
           return  LdFormat.ObjectToString( oBase.ExecuteScalar(oCmd));

         
        }


        public DataTable GetParagraph(string CaseId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
            select ParagraphText ,ParagraphTypeId  from  t_Case_ParagraphLogic where CaseId=@CaseId
     and ParagraphTypeId in (140,150)
");

            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CaseId", DbType.String, CaseId);
            DataSet oSet = oBase.ExecuteDataSet(oCmd);

            if (oSet.Tables.Count > 0)
            {
                return oSet.Tables[0];
            }
            return null;


        }



        /// <summary>
        /// 添加索引后更改状态
        /// </summary>
        /// <param name="caseId"></param>
        /// <returns></returns>
        public bool UpdateIndexStatus(int CaseId)
        {
            Database oBase = DatabaseFactory.CreateDatabase("Caselibrary");
            DbCommand oCmd = oBase.GetSqlStringCommand("Update t_Case set Is_Index=1 where CaseId=@CaseId");
            oBase.AddInParameter(oCmd, "@CaseId", DbType.Int32, CaseId);
            int reValue = oBase.ExecuteNonQuery(oCmd);

            if (reValue > 0)
            {
                return true;
            }
            return false;
        }
     

      


    }
}
